import type Node from "element-plus/es/components/tree/src/model/node";
interface Tree {
  label: string;
  value: string;
  leaf: boolean;
  level: number;
}

export interface TreeDataProp {
  selectValue: string;
}

export function useTreeData() {
  let treeData = ref<TreeDataProp>({
    selectValue: ""
  });

  const loadNode = async (node: Node, resolve: (data: Tree[]) => void) => {
    const data = await dictionaryTypeList({ code: useGlobalStore().systemCode });
    let newData = [];
    const { level } = node;
    if (level === 0) {
      for (let item of data) {
        newData.push({
          label: item,
          leaf: false,
          value: "",
          level
        });
      }

      return resolve(newData);
    }

    if (level === 1) {
      let data = await dictionaryCodeListByType({ type: node.data.label });

      const newData = data.map(item => {
        const { dcode, remark } = item;

        return {
          label: remark,
          value: dcode,
          leaf: true,
          level
        };
      });
      return resolve(newData);
    }
  };
  return {
    treeData,
    loadNode
  };
}
